package demo.hello;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class Producer {
    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException,
            MQBrokerException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
        String nameSrvAddr = "10.1.5.9:9876";
        producer.setNamesrvAddr(nameSrvAddr);
        producer.setSendMsgTimeout(10000);
        producer.start();

        Message msg = new Message("TopicTest2", "TagA", "key113",
                ("Hello world3"+new Date()).getBytes(RemotingHelper.DEFAULT_CHARSET));
        SendResult sendResult = producer.send(msg);
        System.out.printf("%s%n", sendResult);

        // QueryResult queryMessage =
        // producer.queryMessage("TopicTest1", "key113", 10000, 0,
        // System.currentTimeMillis());
        // for (MessageExt m : queryMessage.getMessageList()) {
        // System.out.printf("%s%n", m);
        // System.out.println(new String(m.getBody()));
        // }
        producer.shutdown();

    }
}
